<?php
namespace app\admin\controller;

use \app\common\model\ParametersLottery;
use app\common\component\parameters\ParametersModel;
use app\common\component\parameters\ParametersService;

class System extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
		$parametersService = new ParametersService();
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			foreach($post as $k => $v) {
				if(getParam($k) != $v) {
					$getParameter = ParametersModel::get(['key' => $k]);
					switch($getParameter -> type) {
						case 4:
							$v = json_encode($v);
							break;
						default:
							$v = trim($v);
							break;
					}
					$data['value'] = $v;
					$parametersService -> update($data, $k);
				}
			}
			clearParam();

			echo '<meta charset="utf-8"><script>alert("修改成功!");window.location.href="/shuashuale/system";</script>';exit;
		}

		$getParameters = $parametersService -> getParameters();

		$this -> view -> assign('parameters', $getParameters['data']['parameters']);

		return $this -> view -> fetch();
	}

	public function pay() {
		$parametersService = new ParametersService();
		if($this -> request -> isPost()) {
			if(request() -> file('MONEY_ALIPAY_PIC')) {
				$parametersService -> update(['value' => uploadFile('MONEY_ALIPAY_PIC')], 'MONEY_ALIPAY_PIC');
			}
			if(request() -> file('MONEY_WECHATPAY_PIC')) {
				$parametersService -> update(['value' => uploadFile('MONEY_WECHATPAY_PIC')], 'MONEY_WECHATPAY_PIC');
			}

			echo '<meta charset="utf-8"><script>alert("修改成功!");window.location.href="/shuashuale/system/pay";</script>';exit;
		}

		$getParameters = $parametersService -> getParameters();

		$this -> view -> assign('parameters', $getParameters['data']['parameters']);

		return $this -> view -> fetch();
	}

	public function clear() {
		$this -> view -> assign('code', exec('/www/wdlinux/php/bin/php ' . ROOT_PATH . 'think clear'));
		//$this -> view -> assign('code', exec('php ' . ROOT_PATH . 'think clear'));
		return $this -> view -> fetch();
	}

	public function lottery() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			foreach($post as $k => $v) {
				$v = trim($v);
				if(getParamLottery($k) != $v) {
					ParametersLottery::where('key', $k) -> update(['value' => $v]);
				}
			}

			clearParamLottery();
			echo '<meta charset="utf-8"><script>alert("修改成功!");window.location.href="/shuashuale/system/lottery";</script>';exit;
		}
		$this -> view -> assign('parameters', ParametersLottery::all());
		return $this -> view -> fetch();
	}
}